package com.zhuss.car.processor;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.zhuss.bean.bo.core.upload.DownloadBo;
import com.zhuss.bean.vo.core.upload.DownloadVo;
import com.zhuss.common.Processor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import com.zhuss.bean.bo.car.car.SearchCarBo;
import com.zhuss.bean.vo.car.car.DetailCarVo;
import com.zhuss.bean.vo.car.car.ListCarVo;
import com.zhuss.car.db.dao.ext.CarDao;
import com.zhuss.car.db.entity.Car;
import com.zhuss.common.ListVo;
import com.zhuss.common.PageVo;
import com.zhuss.core.db.dao.ext.CompanyDao;
import com.zhuss.core.db.entity.Company;
import com.zhuss.exception.DzcxException;
import com.zhuss.exception.ExceptionCode;


/**
 * @description: 车辆读
 * @author: zhushanshuo
 * @create: Fri Dec 18 20:35:18 CST 2020
 **/
@Component("carRead")
public class CarRead {

    @Autowired
    private CarDao carDao;

    @Autowired
    private CompanyDao companyDao;

    @Autowired
    @Qualifier("downloadProcessor")
    private Processor<DownloadVo, DownloadBo> downloadProcessor;


    public Map<String, Car> listMap() {
        List<Car> list = carDao.listAll();
        Map<String, Car> map = new HashMap<>();
        for (Car c : list) {
            map.put(c.getPlateNo(),c);
        }
        return map;
    }

    /**
     * 车辆
     *
     * @return
     */
    public List<Car> listCar() {
        List<Car> result = carDao.listAll();
        return result;
    }

    /**
     * 车辆
     *
     * @return
     */
    public Object listCarSelect() {
        List<Car> carList = listCar();
        List<ListVo> result = new ArrayList<>();
        transferList(carList,result);
        return result;
    }


    /**
     * 车辆
     *
     * @return
     */
    public Object listCar(List<Long> carIdList) {
        List<Car> result = carDao.listById(carIdList);
        return result;
    }


    /**
     * 根据名字查询车辆
     *
     * @param
     * @return
     */
    public List<Car> list(String name) {
        List<Car> result = carDao.listByName(name);
        return result;
    }

    public List<Car> listNotId(String name,Long carId) {
        List<Car> result = new ArrayList<Car>();
        List<Car> list = list(name);
        for (Car c : list) {
            if (!c.getCarId().equals(carId)) {
                result.add(c);
                break;
            }
        }
        return result;
    }

    /**
     * 车辆
     *
     * @return
     */
    public Object listCarId() {
        List<Car> carList = carDao.listAll();
        List<Long> result = new ArrayList<>();
        transferListId(carList,result);
        return result;
    }

    private void transferListId(List<Car> carList,List<Long> result) {
        for (Car item : carList) {
            result.add(item.getCarId());
        }
    }

    private void transferList(List<Car> carList,List<ListVo> result) {
        for (Car item : carList) {
            ListVo vo = new ListVo();
            vo.setKey(item.getCarId());
            vo.setLabel(item.getPlateNo());
            result.add(vo);
        }

    }

    /**
     * 车辆列表
     *
     * @param bo
     * @return
     */
    public Object list(SearchCarBo bo) throws DzcxException {
        PageVo<ListCarVo> result = new PageVo<ListCarVo>();
        result.setPageNum(bo.getPageNum());
        result.setPageSize(bo.getPageSize());
        List<ListCarVo> carVoList = carDao.list(bo);
        //公司id映射成英文
        Map<Long, String> map = new HashMap<>();
        List<Company> companyList = companyDao.listAll();
        companyList.forEach(company -> {
            map.put(company.getCompanyId(),company.getName());
        });

        for (ListCarVo vo : carVoList) {
            if (vo.getCompanyId() != null) {
                vo.setCompanyName(map.get(vo.getCompanyId()));
            }
            vo.setPicUrl(genUrl(vo.getPic()));
        }

        result.setItems(carVoList);
        result.setTotal(carDao.countList(bo));
        return result;
    }

    /**
     * 车辆详情
     *
     * @param carId
     * @return
     * @throws DzcxException
     */
    public DetailCarVo detail(Long carId) throws DzcxException {
        DetailCarVo vo = carDao.detail(carId);
        if (vo == null) {
            throw new DzcxException(ExceptionCode.数据不存在);
        } else {
            if (vo.getCompanyId() != null) {
                Map<Long, String> map = new HashMap<>();
                List<Company> companyList = companyDao.listAll();
                companyList.forEach(company -> {
                    map.put(company.getCompanyId(),company.getName());
                });
                vo.setCompanyName(map.get(vo.getCompanyId()));

            }
           vo.setPicUrl(genUrl(vo.getPic()));
        }

        return vo;
    }

    private String genUrl(String img) throws DzcxException {
        if (img == null || "".equals(img.trim())) {
            return null;
        }
        DownloadBo downloadBo = new DownloadBo();
        downloadBo.setKey(img);
        DownloadVo downloadVo = downloadProcessor.process(downloadBo);
        return downloadVo.getUrl();
    }


    /**
     * 车辆下拉
     *
     * @param bo
     * @return
     * @throws DzcxException
     */
    public Object selectByType(SearchCarBo bo) throws DzcxException {
        if (bo.getRideType() == null || bo.getRideType() == 0) {
            return new DzcxException(ExceptionCode.出行方式为空);
        }
        if (bo.getType() == null || bo.getType() == 0) {
            return new DzcxException(ExceptionCode.车辆类型为空);
        }
        return carDao.selectByType(bo);
    }


}
